package com.ljy.tkuser.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ljy.tkuser.entity.LjyBigUser;
import com.ljy.tkuser.service.LjyBigUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@Component
public class UserCookieRedis {

    @Autowired
     LjyBigUserService ljyBigUserService;
    @Autowired
    RedisTemplate redisTemplate;
    public  LjyBigUser getUserCookie(){

        String encode = (String) redisTemplate.opsForValue().get("userCookie");

        String decode = null;
        try {
            decode = URLDecoder.decode(encode, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONObject.parseObject(decode);
        LjyBigUser ljyBigUser = JSON.toJavaObject(jsonObject, LjyBigUser.class);
        if (StringUtils.isEmpty(ljyBigUser)){
            //为空时应该前端调登录
            try {
                throw new Exception("为空时应该前端调登录");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ljyBigUser;
    }
}
